package com.deng.config.shiroConfig;

import com.deng.Service.PermissionService;
import com.deng.Service.RoleService;
import com.deng.Service.UserService;
import com.deng.model.To.UserAndRoles;
import com.deng.model.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.logging.Logger;

/**
 * @Author 邓小熙
 * @Date 2022/5/3 10:11
 * @Version 1.0
 */
public class MyRealm extends AuthorizingRealm {
    @Autowired
    UserService userService;
    @Autowired
    PermissionService permissionService;
    @Autowired
    RoleService roleService;
//    Logger.getLogger(this.getClass());
    Logger logger = Logger.getLogger(String.valueOf(this.getClass()));
//Logger logger = Logger.getLogger(this.getClass().getName());
//   认证
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

//        拿到当前登录的对象
        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        UserAndRoles userAndRoles = new UserAndRoles();
        String username = user.getLogin_name();
        logger.info(username);
//        设置当前用户的权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(permissionService.findPermissionByName(username));
        System.out.println(permissionService.findPermissionByName(username));
        logger.info("permission"+permissionService.findPermissionByName(username));
        simpleAuthorizationInfo.setRoles(roleService.findRoleByName(username));
        System.out.println(roleService.findRoleByName(username));
        logger.info("roles"+roleService.findRoleByName(username));
        return simpleAuthorizationInfo;
//        return (AuthorizationInfo) simpleAuthenticationInfo;
    }
//      登录
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
//        通过用户名到数据库中获取凭证
        User user = userService.findUserByName(username);
        if (user.getStatus()==0){
            logger.info("该用户账号已冻结");
        }else {
            logger.info("用户正常登录");
        }


        String password = user.getPassword();
//        使用md5加密
        return new SimpleAuthenticationInfo(user,password,this.getClass().getName());
        }
    /**
     * 使用md5算法加密
     * @param credentialsMatcher
     */
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        HashedCredentialsMatcher md5CredentialsMatcher = new HashedCredentialsMatcher();
        md5CredentialsMatcher.setHashAlgorithmName(ShiroUtil.hashAlgorithmName);
//        md5CredentialsMatcher.setHashIterations(ShiroUtil.hashIterations);
        md5CredentialsMatcher.setHashIterations(1);

        super.setCredentialsMatcher(md5CredentialsMatcher);
    }
}
